package com.example.demo.config;
import com.ulisesbocchio.jasyptspringboot.properties.JasyptEncryptorConfigurationProperties;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.salt.ZeroSaltGenerator;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(JasyptEncryptorConfigurationProperties.class)
public class MyEncryptorConfig {
    /**
     * 默认找jasyptStringEncryptor这个bean，这里直接取这个名字进行注册，省去额外配置
     * @param properties
     * @return
     */
   @Bean
    StringEncryptor jasyptStringEncryptor(JasyptEncryptorConfigurationProperties properties){
        return new StringEncryptor() {
            // 加密
            @Override
            public String encrypt(String message) {
                // todo加密实现
                return message;
            }

            // 解密
            @Override
            public String decrypt(String encryptedMessage) {
                StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
                standardPBEStringEncryptor.setPassword(properties.getPassword());
                standardPBEStringEncryptor.setSaltGenerator(new ZeroSaltGenerator());
                return standardPBEStringEncryptor.decrypt(encryptedMessage);
            }
        };
    }
}
